package eiisan.service.user.impl;

import eiisan.constant.enums.user.UserStatusEnum;
import eiisan.dao.domain.user.Permission;
import eiisan.dao.domain.user.User;
import eiisan.dao.repository.user.UserRepository;
import eiisan.dao.result.user.PermissionInfo;
import eiisan.dao.result.user.RoleInfo;
import eiisan.dao.result.user.UserDetail;
import eiisan.service.user.AuthorityService;
import eiisan.util.model.response.Result;
import eiisan.util.reflect.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 权限信息服务
 *
 * @author gragonfly
 * @date 2019/7/6
 **/
@Slf4j
@Service
@Transactional
public class AuthorityServiceImpl implements AuthorityService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public Result<UserDetail> getUserDetail(Long userId) {
        try {
            User user = userRepository.getOne(userId);
            if (user == null) {
                return Result.fail("账号不存在");
            }
            if (user.getStatus().equals(UserStatusEnum.FORBIDDEN.getVal())) {
                return Result.fail("账号已停用，请联系管理员");
            }
            UserDetail userDetail = new UserDetail();
            BeanUtils.copyProperties(user, userDetail);
            RoleInfo roleInfo = BeanUtil.copy(user.getRole(), RoleInfo.class);
            Set<Permission> userPermissions = user.getRole().getPermissions();
            Set<PermissionInfo> permissions = new HashSet<PermissionInfo>();
            for(Permission up : userPermissions){
                PermissionInfo permissionInfo = BeanUtil.copy(up, PermissionInfo.class);
                permissions.add(permissionInfo);
            }
            userDetail.setRoleInfo(roleInfo);
            userDetail.setPermissions(permissions);
            return Result.success(userDetail);
        } catch (Exception e) {
            log.error("[getUserDetail]", e);
            return Result.errorServe();
        }
    }
    @Override
    public Result<UserDetail> getUserDetailByUsername(@NotBlank String userName) {
        try {
            User user = userRepository.findByName(userName);
            if (user == null) {
                return Result.fail("未查询到用户信息!");
            }
            if (user.getStatus().equals(UserStatusEnum.FORBIDDEN.getVal())) {
                return Result.fail("用户已禁用!");
            }
            UserDetail userDetail = new UserDetail();
            BeanUtils.copyProperties(user, userDetail);
            RoleInfo roleInfo = BeanUtil.copy(user.getRole(), RoleInfo.class);
            Set<Permission> userPermissions = user.getRole().getPermissions();
            Set<PermissionInfo> permissions = new HashSet<PermissionInfo>();
            for(Permission up : userPermissions){
                PermissionInfo permissionInfo = BeanUtil.copy(up, PermissionInfo.class);
                permissions.add(permissionInfo);
            }
            userDetail.setRoleInfo(roleInfo);
            userDetail.setPermissions(permissions);
            return Result.success(userDetail);
        } catch (Exception e) {
            log.error("[getUserDetailByUsername]", e);
            return Result.errorServe();
        }
    }

    @Override
    public Result getUserDetailByNames(@NotEmpty List<String> userNames, boolean s) {
        try {
            List<User> users = userRepository.findByNameIn(userNames);

            return Result.success(users);
        } catch (Exception e) {
            log.error("[getUserInfosByNames]", e);
            return Result.errorServe();
        }
    }

}
